package com.lxf.cn.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lxf.cn.model.User;
import com.lxf.cn.service.UserService;
import com.lxf.cn.uitl.export.ExportExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by liaoxuefeng on 2018/11/26.
 */

@Controller
@RequestMapping("excel")
public class ExcelExportController {


    @Autowired
    UserService userService ;

    /**
     *
     * @param request
     * @param response
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/export/test", method = RequestMethod.GET , produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void excelExportOrder(HttpServletRequest request , HttpServletResponse response , User user) throws Exception {

        String [] headers = new String[]{"用户名" };

        List<Map> list =userService.getExcleDate();

        JSONArray object = new JSONArray();
        object.addAll(list);

        XSSFWorkbook wb = ExportExcelUtil.genInputStream("excelTemplate/test.xlsx", object , headers) ;
        exportXSL("测试列表", wb, response);

    }


    /**
     * 导出excel
     *
     * @param filename  导出文件名
     * @param wb
     */
    public void exportXSL(String filename, XSSFWorkbook wb, HttpServletResponse response) {
        export("xlsx", filename, wb, response);
    }


    /**
     * 导出执行方法
     *
     * @param format   //文件格式
     * @param filename //文件名
     * @param wb
     * @return
     */
    public void export(String format, String filename, XSSFWorkbook wb, HttpServletResponse response) {
        filename = filename + new SimpleDateFormat("[yyyy-MM-dd]").format(new Date()) + "." + format;

        try {
            response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes(), Charset.forName("ISO-8859-1")));
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            wb.write(bos);
            wb.close();
        } catch (Exception e) {
            throw  new RuntimeException("导出文件失败", e);
        }
    }

    /**
     * 导出错误信息文件
     *
     * @return
     */
    public void exportErrorFile(String errorMsg, HttpServletResponse response) {

        try {
            response.setHeader("Content-Disposition", "attachment; filename=" + new String("错误信息.txt".getBytes(), Charset.forName("ISO-8859-1")));
            OutputStream out= response.getOutputStream();
            out.write(errorMsg.getBytes());
            out.close();
        } catch (Exception e) {
            throw  new RuntimeException("导出文件失败", e);
        }
    }

}
